//
//  TabBarController.m
//  diaoyuren
//
//  Created by 钓鱼人 on 15/12/18.
//  Copyright © 2015年 game. All rights reserved.

#import "TabBarController.h"
#import "MainPageController.h"
#import "IslandsController.h"
#import "PlusSignController.h"
#import "SchoolController.h"
#import "PersonageController.h"
#import "LoginController.h"
#import "PlusSignController.h"
#import "PlushAnimation.h"
#import "MyTabBar.h"
#import "ChangyanSDK.h"
#import "RKNotificationHub.h"
#import "UITabBar+CustomBadge.h"
#import "UserDataModel.h"
#import "HYBModalTransition.h"

@interface TabBarController ()<UITabBarDelegate,MyTabBarDelegate>
{
    UINavigationController *MainPageNav;
    UINavigationController *IslandsNav;
    UINavigationController *PlusSignnav;
    UINavigationController *SchoolNav;
    UINavigationController *PersonageNav;
     RKNotificationHub *_barHub;
}

@end

@implementation TabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.transitioningDelegate = self;
    //设置系统导航栏背景图
    [[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"nav-background"] forBarMetrics:0];
//
//    //设置tabbar子控制器
    [self addChildVc:[[MainPageController alloc] init] title:@"首页" image:@"shouye" selectedImage:@"shouye1"];

    [self addChildVc:[[IslandsController alloc]init] title:@"钓场" image:@"diaochang" selectedImage:@"diaochang1"];


    [self addChildVc:[[SchoolController alloc] init] title:@"学堂" image:@"xuetang" selectedImage:@"xuetang1"];
    
    [self addChildVc:[[PersonageController alloc] init] title:@"我" image:@"wo" selectedImage:@"wo1"];
    
    MyTabBar *tabBar = [[MyTabBar alloc] init];//添加Button设置
    tabBar.delegate = self;
    
    [self setValue:tabBar forKey:@"tabBar"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(NotificationdidUpdate:) name:@"LoginNotifi" object:nil];
    
     [self LoginAction];

}

-(void)NotificationdidUpdate:(NSNotification *)notification{
    
    UserDataModel *model = notification.userInfo[@"sss"];
    
    int count = model.pm_members_count.intValue+model.mypost_notify_count.intValue+ model.interactive_notify_count.intValue+model.system_notify_count.intValue;
  
    
    if (count==0) {
        [self.tabBar setBadgeStyle:kCustomBadgeStyleNone value:10 atIndex:3];

    }else{
    
        [self.tabBar setBadgeStyle:kCustomBadgeStyleRedDot value:10 atIndex:3];
    }

    
    
}

#pragma mark---导航栏设置封装方法
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    childVc.title = title;
    
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    //微调tabbar文字与图表的位置
     childVc.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -7);
    childVc.tabBarItem.imageInsets = UIEdgeInsetsMake(-3, 0, 3, 0);
    
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //未选中时文字颜色（正常颜色）
    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName :RGBA(123, 123, 123, 1)} forState:UIControlStateNormal];
    //选中时文字颜色（绿色）
    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : mainColor} forState:UIControlStateSelected];
    
    UINavigationController *navigationVc = [[UINavigationController alloc] initWithRootViewController:childVc];
    
    [self addChildViewController:navigationVc];
}


#pragma MyTabBarDelegate 加号按钮点击
- (void)tabBarDidClickPlusButton:(MyTabBar *)tabBar
{
    [PlushAnimation showMenu];
}

-(void)LoginAction{

    /**
     *  自动登录  如果十天之内没有进过应用或没有登录成功 将不会自动登录
     */
    NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
    NSTimeInterval time = [[NSDate date] timeIntervalSinceDate:[ud valueForKey:@"time"]];
    CGFloat day=time/(3600*24);
    if ([ud valueForKey:@"userName"] && day<10) {
        
        if ([ud valueForKey:@"userName"]!=nil) {
            AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
            app.isLogin = @"YES";
            [ud setObject:[NSDate date] forKey:@"time"];
            
        }
    }
}

#pragma mark - UIViewControllerTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    return [HYBModalTransition transitionWithType:kHYBModalTransitionPresent duration:0.5 presentHeight:screen_width scale:CGPointMake(1, 1)];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    return [HYBModalTransition transitionWithType:kHYBModalTransitionDismiss duration:0.25 presentHeight:screen_width scale:CGPointMake(1, 1)];
}

@end
